package org.massmoderation.com.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Configuration extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    
    private static String confFile = null;
    private static Properties settings = null;
    private static Map<String, String> configurationMap;

    @Override
    public void init(ServletConfig config) throws ServletException
    {
        System.out.println("Deploying MassModeration");
        
        confFile = config.getInitParameter(ConfConstants.CONFIGURATION_FILE);

        System.out.println("MassModeration[ conffile: " + confFile );
        
        load();

        System.out.println("Initialized...");
    }

    /**
     * Caricamento delle chiavi di configurazione a partire dal confFile.
     */
    private void load()
    {
        settings = new Properties();
        configurationMap = new HashMap<String, String>();
        FileInputStream stream = null;
        
        try
        {
            stream = new FileInputStream(confFile);
            settings.load(stream);

            addAttribute(ConfConstants.MIN_RATE_COMMENT);
            addAttribute(ConfConstants.MIN_RATE_POI);
            addAttribute(ConfConstants.A_COMMENT);
            addAttribute(ConfConstants.A_POI);
            addAttribute(ConfConstants.B_COMMENT);
            addAttribute(ConfConstants.B_POI);
            addAttribute(ConfConstants.C);
            addAttribute(ConfConstants.C_COMMENT);
            addAttribute(ConfConstants.C_POI);
            addAttribute(ConfConstants.D_COMMENT);
            addAttribute(ConfConstants.D_POI);
            addAttribute(ConfConstants.DB_PASSWD);
            addAttribute(ConfConstants.DB_URL);
            addAttribute(ConfConstants.DB_USER);
            addAttribute(ConfConstants.KC);
            addAttribute(ConfConstants.KP);
            addAttribute(ConfConstants.N);
            addAttribute(ConfConstants.NEGATIVE);
            addAttribute(ConfConstants.POSITIVE);
            addAttribute(ConfConstants.T);
            addAttribute(ConfConstants.TIME_LIMIT_DATA);
            addAttribute(ConfConstants.TIME_LIMIT_USER);
            addAttribute(ConfConstants.TOKEN_FOR_ALL);
            addAttribute(ConfConstants.TOKEN_LIMIT_TIME);
            addAttribute(ConfConstants.TOKEN_FOR_NEGATIVE_COMMENT);
            addAttribute(ConfConstants.TOKEN_FOR_NEGATIVE_POI);
            addAttribute(ConfConstants.TOKEN_FOR_POSITIVE_COMMENT);
            addAttribute(ConfConstants.TOKEN_FOR_POSITIVE_POI);
            addAttribute(ConfConstants.TOTAL);
            addAttribute(ConfConstants.U);

        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }        
        finally
        {
            // chiusura dello stream al termine
            if (stream != null) 
                try { stream.close(); }
                catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        load();
        System.out.println("Reloaded...");
    }
    
    public static String getConfFile() {
        return confFile;
    }

    public static void setConfFile(String confFile) {
        Configuration.confFile = confFile;
    }

    public static Properties getSettings() {
        return settings;
    }

    public static void setSettings(Properties settings) {
        Configuration.settings = settings;
    }
    
    
    //restituisce il valore passato su key
    public static String getParameter(String key) {
        return Configuration.configurationMap.get(key);
    }
    
    private void addAttribute(String item) {
        configurationMap.put( item, (String) settings.get( item ) );
    }
}